home *** CD-ROM | disk | FTP | other *** search
/ Programmers Heaven 2 / Programmers Heaven 2.iso / files / graphics / library / wgt51_r2.zip / WGT5 / DOC / WATCOM.07 < prev    next >
Text File  |  1996-08-02  |  18KB  |  427 lines

  1.  
  2.                    The WATCOM C/C++ Game Programmer's FAQ
  3.  
  4.  
  5. ||        ||   ||   |||||||| ||||    ||||      ||  ||    cccc  /
  6. ||        ||  ||||     ||   ||  ||  ||  ||    ||||||||  cc    /
  7. ||        || ||  ||    ||  ||      ||    ||  || |||| ||  cccc/
  8. ||        || ||  ||    ||  ||      ||    ||  ||  ||  ||     /
  9.  ||  ||  || ||    ||   ||  ||      ||    || ||   ||   ||   /
  10.  || |||| || ||||||||   ||  ||      ||    || ||   ||   ||  /  cccc   +   +
  11.   |||||||| ||      ||  ||   ||  ||  ||  ||  ||   ||   ||    cc     +++ +++
  12.    ||  ||  ||      ||  ||    ||||    ||||   ||   ||   ||     cccc   +   +
  13.  
  14.                              Game Programmer's
  15.  
  16.  
  17.    ***********************   *******               **************
  18.    ***********************  *********            *******    *******
  19.    ********                ***********         *******         *******
  20.    ********               *************        *******         *******
  21.    *****************     ******* *******       *******         *******
  22.    *****************    *******   *******      *******         *******
  23.    ********            *******     *******     *******         *******
  24.    ********           *********************    *******    ************
  25.    ********          ***********************     *******************
  26.    ********         *******           *******       *********************
  27.                                                                     *******
  28.  
  29. Created October 23, 1994 by Lee A. Lorenz
  30. Version 0.7
  31.  
  32. ==============================================================================
  33. HISTORY:
  34.  
  35.  Version    Date            Description
  36. ---------+----------+--------------------------------------------------------
  37.   0.5    | 10/23/94 | Initial version, skimpy, yes, but at least here it is!
  38.          |          | Not much, just to get it started. I will add to it, but
  39.          |          | others could add to it as well...
  40. ---------+----------+--------------------------------------------------------
  41.   0.6    | 10/24/94 | A little more added.
  42. ---------+----------+--------------------------------------------------------
  43.   0.7    | 10/29/94 | Table of Contents added (uses Line#, though).
  44. ---------+----------+--------------------------------------------------------
  45.  
  46. ==============================================================================
  47.  
  48.                              Table Of Contents
  49.                                                             Line Numbers
  50. Data sizes in protected mode.....................................110
  51. MK_FP doesn't work the same......................................128
  52. Hooking Interrupts...............................................151
  53. Finding WATCOM files (FTP sites, BBS)............................197
  54. Libraries for WATCOM C/C++32.....................................218
  55. Problems linking non-WATCOM object files.........................249
  56. Using Inline assembly............................................285
  57. Books for WATCOM C/C++32.........................................319
  58. DOS extenders for WATCOM C/C++32.................................325
  59. Differences in using assembly....................................345
  60. Using DPMI.......................................................359
  61. Accessing VESA BIOS functions....................................367
  62. Trying to recompile other source code............................390
  63.  
  64. ==============================================================================
  65.  
  66. Well, since the "Betrayal" at Borland, and WATCOM's strategic pricing of
  67. WATCOM C/C++ version 10.0, many users unfamiliar with protected mode
  68. programming have been posting alot of questions. The purpose of this FAQ
  69. is to clear up many of those questions. For now, I will concentrate on the
  70. specifics of programming WATCOM C/C++ (9.5 or 10.0) in protected (or 32-bit)
  71. mode, using the DOS/4GW Dos Extender from Rational Systems.
  72.  
  73.     To begin this FAQ, I'd like to outline my own reasons for switching to
  74. WATCOM's C/C++ compiler.
  75.     I've been a longtime Borland C/C++ user, in fact, I was a registered
  76. user from version 1.0 on up to 3.1. About two years ago, the need for a
  77. good 32-bit compiler began surfacing with my growing requirements for
  78. memory and the complications of segmented programming became apparent.
  79.     In November of 1993, Borland released version 4.0- but while it was
  80. advertised as a "32-bit" compiler, this was only true of Windows
  81. programming. Other disturbing things happened- Borland announced that
  82. they would not support TurboVision, they withdrew the DOS IDE, they
  83. seperated TASM, to make it an additional purchase. Still, I was reassured by
  84. Borland (via Paul Gross himself on CompuServe) that Borland might still
  85. upgrade the package to include better DOS tools (including 32-bit
  86. development). Until they did that, the appearance was that Borland was saying
  87. that DOS development was DEAD AND BURIED.
  88.     I waited... and waited... until I saw an ad in Dr.Dobb's Journal for
  89. the PowerPak in June of 1994. Great... but when I called to upgrade my
  90. Borland C/C++ to 4.0 and order the PowerPak (now $199+$99 for powerpak),
  91. the operators didn't even know about it! Even worse was after a few days,
  92. they told me it *MIGHT* ship in July, but they weren't sure!
  93.     That was when I saw WATCOM's ad, advertising the special $199 price. I
  94. called The Programmer's Shop and ordered it. They told me 10.0 wasn't
  95. shipping yet, HOWEVER, they would send me version 9.5. Cool. Better still,
  96. while the $199 price did not include printed manuals (CD-ROM docs), the
  97. 9.5 sent to me *DID* have all of the manuals.
  98.     Well, now I'm a convert... it doesn't hurt that one of the hottest
  99. PC games ever, DOOM, was written using WATCOM C/C++ and the DOS/4GW
  100. extender, as well as many other leading games (X-COM, Indy Car Racing).
  101. My next task was to port many of my libraries over to WATCOM C, luckily,
  102. I had dabbled with the DJGPP release of GNU C/C++, a freeware 32-bit C
  103. compiler, so I had a bit of code already converted.
  104.  
  105. -------------------------------------------------------------------------------
  106.  
  107. Now for the questions:
  108.  
  109. -------------------------------------------------------------------------------
  110.  
  111. Q. My structures appear to be all messed up. Loading from files directly into
  112.    the structures gets "shifted" and the wrong values show up. What's
  113.    happening?
  114.  
  115. A. Well, if you have declared "int" in your structures, bear in mind that
  116.    the size of the int has CHANGED. You need to change them to "short".
  117.  
  118.     TYPE            Size in 16-bit compiler         Size in 32-bit compiler
  119.     char              8 bits  (1 byte)                 8 bits  (1 byte)
  120.     int              16 bits  (2 bytes)               32 bits  (4 bytes)
  121.     short            16 bits  (2 bytes)               16 bits  (2 bytes)
  122.     long             32 bits  (4 bytes)               32 bits  (4 bytes)
  123.  
  124.    Thus, use "short" and "long" where the size of the data is specifically
  125.    required.
  126.  
  127. -------------------------------------------------------------------------------
  128.  
  129. Q. How come this doesn't work anymore for accessing video memory:
  130.  
  131.     unsigned char far *ScrPtr;
  132.  
  133.     ScrPtr=MK_FP(0xa000,0);
  134.  
  135. A. Well, that's because memory is no longer segmented. In 32-bit mode, we use
  136.    "selectors" wich refer to chunks of memory defined in GDTs (Global
  137.    Descriptor Tables). In DOS/4GW, the "default" selector maps the first
  138.    megabyte of memory (your traditional DOS memory) to the first linear
  139.    megabyte of addresses. In short, the screen memory for graphics is located
  140.    at 0x000a0000 through 0x000affff. So, our program needs the following:
  141.  
  142.     unsigned char *ScrPtr;               /* No "far" required here */
  143.  
  144.     ScrPtr=(unsigned char *)0x0a0000;
  145.  
  146.    The reason the call to MK_FP() is allowed, is that there will be cases where
  147.    you need "far" pointers, as in the case of DPMI calls (see below). In this
  148.    case, a selector is a 16-bit value and the offset if a 32-bit value.
  149.  
  150. -------------------------------------------------------------------------------
  151.  
  152. Q. I don't understand "bimodal" interrupts, how am I going to hook into INT9
  153.    to capture keys with my own keyhandler?
  154.  
  155. A. The interrupt mechanism for protected mode is quite complicated. When an
  156.    interrupt is called, it may call one of TWO possible routines, either a
  157.    "Real-mode" routine or a "Protected-mode" routine, depending on which
  158.    state the CPU was in. To deal with this, DOS/4GW has a feature known as
  159.    "auto-passup/auto-passdown". Simply put, if the interrupt function is
  160.    defined for Real-mode, and not in Protected-mode, and the interrupt is
  161.    called in protected mode, DOS/4GW will pass it "down" to the Real-mode
  162.    function.
  163.     Now the kicker... if you define a Protected-mode function for that
  164.    interrupt, DOS/4GW will pass all interrupts occuring in Real-mode *UP*
  165.    to Protected-mode!
  166.     There is a price to pay for this feature, speed. There is a little bit of
  167.    overhead involved. In many cases, this is not a problem, so attempt this
  168.    method first before trying a bimodal interrupt.
  169.     Here is a shell of implementing an IRQ handler:
  170.  
  171.  
  172.     void (__interrupt __far *OldISR)();
  173.  
  174.     void interrupt ISR_0( void )
  175.     {
  176.         /* Your code here... */
  177.     }
  178.  
  179.     void main(void)
  180.     {
  181.             :               :
  182.         OldISR=_dos_getvect(TheVectorHere);
  183.         _dos_setvect(TheVectorHere,ISR_0);
  184.             :               :
  185.         _dos_setvect(TheVectorHere,OldISR);
  186.  
  187.     }
  188.  
  189.     That's all there is to it! Remember all of the normal things involved
  190.    with hooking into IRQs. Also, as the old vector (protected mode)
  191.    redirects the IRQ to Real mode, it is possible to chain back through
  192.    using it.
  193.  
  194.     The "auto-passup" range are INTs 08H to 02EH, excluding 021H (DOS)
  195.  
  196. -------------------------------------------------------------------------------
  197.  
  198. Q. Where can I find Watcom-specific files?
  199.  
  200. A. There is a small site run by Ivan Pulleyn at netcom.com:
  201.  
  202.         ftp.netcom.com /pub/hypereal/watcom
  203.  
  204.     You will find patches and other Watcom-related files here.
  205.  
  206.    WATCOM has several ways to get information, files and patches:
  207.     BBS:            (519)884-2103.
  208.     Internet FTP:   ftp.watcom.on.ca
  209.     E-Mail:         tech@watcom.on.ca
  210.     Compu$erve:     GO WATCOM
  211.  
  212.     Less specifically, but equally useful ftp sites:
  213.  
  214.     x2ftp.oulu.fi       /pub/msdos
  215.     ftp.eng.ufl.edu     /demos
  216.  
  217.  
  218. -------------------------------------------------------------------------------
  219. Q. What kind of libraries are available?
  220.  
  221. A. Well, quite a few, with more coming every day.
  222.  
  223.     Commercial:
  224.  
  225.     MetaWindow/386  -   A graphics/windowing library
  226.  
  227.     ShareWare:  (two net sites to try: x2ftp.oulu.fi and ftp.eng.ufl.edu)
  228.  
  229.     WGT 5.0         -   A nice gaming graphics library, includes VESA SVGA
  230.                         functions, joystick and keyboard handling, sprites
  231.                         and more. Currently in Beta release.
  232.  
  233.     TinyPlay 2.11   -   A decent 8-track MOD player/sound effects package.
  234.                         Source is included.
  235.  
  236.     ll_comm         -   "Lord Logic's" Communication code. Good start for
  237.                         writing some basic serial port code.
  238.  
  239.     ll_kbd          -   "Lord Logic's" Keyboard handler.
  240.  
  241.     text_eng        -   Code to warp an image to any 4-sided polygon.
  242.  
  243.  
  244.    I have successfully converted 99% of GifLib 1.2. Perhaps I'll get around
  245.    to posting it sometime.
  246.  
  247.  
  248. -------------------------------------------------------------------------------
  249.  
  250. Q. I've got library "X" in .LIB form. How come the linker keeps giving me
  251.    this "_funcname not found" when "funcname" is in the header files?
  252.  
  253. A. WATCOM uses its own naming conventions for functions, which FOLLOWS the
  254.    function name with an underscore instead of PRECEDING the function name.
  255.    The fix for this is to use #pragma aux cdecl (funcname); for each
  256.    external function. Note that if the .LIB calls other library functions
  257.    (i.e., printf, abs, anything like that) the linker cannot resolve these
  258.    names.                         (Thanks to Joe Ottinger for this one).
  259.                       --------------------------
  260.    WATCOM gives you pretty fine control over the linkage convention.
  261.    For example, you can do
  262.  
  263.    #pragma aux foo "_*"  /* common style */
  264.    #pragma aux foo "*_"  /* WATCOM style */
  265.    #pragma aux foo "*"   /* common asm or HP style */
  266.  
  267.    or even
  268.  
  269.    #pragma aux foo "FOO" /* linked as FOO */
  270.  
  271.    I take advantage of this control to do profiling (through __PRO and
  272.    __EPI) and occasionally to access directly WATCOM's internal functions
  273.    for development and testing.   (Thanks to Charles Fu)
  274.                       --------------------------
  275. As a side note:
  276.  
  277. A.  Using a pre-compiled 16-bit library with a 32-bit compiler can be a very
  278.     risky thing. Some code might work fine (small model?), but the general
  279.     rule is that it must be recompiled. Also, some parameters won't match
  280.     correctly (short->int->long problem). With straight C, this is pretty
  281.     easy to convert, but inline assembly and .ASM files require greater
  282.     conversion. (See below)
  283.  
  284. -------------------------------------------------------------------------------
  285.  
  286. Q. How do I use inline assembly? Why doesn't my old Borland C/C++ program
  287.    compile correctly with asm { }?
  288.  
  289. A. Inline assembly is quite different in WATCOM C/C++ than in other
  290.    compilers. The "asm {}" construct is in the ANSI standard, but may
  291.    (according to the standard) simply treat it as comment, which WATCOM
  292.    does.
  293.     The mechanism for inline assembly in WCC++ is using the #pragma aux.
  294.    The advantage is that registers are defined for input and output, and
  295.    the compiler will optimize the code generated (something Borland and MSC
  296.    don't do). Here is an example:
  297.  
  298. unsigned short int10(short,short,short,short);
  299.  
  300. #pragma aux int10 =             \
  301.     "int 10H"                   \
  302.     parm [ax] [bx] [cx] [dx]    \
  303.     value[ax];
  304.  
  305. OR:
  306.  
  307. unsigned char GetRow(void);
  308.  
  309. #pragma aux GetRow =            \
  310.     "mov ax,0300H"              \
  311.     "mov bh,0"                  \
  312.     "int 10H"                   \
  313.     modify [ax bx cx dx]        \
  314.     value [dh];
  315.  
  316.     Now, you need only use it like a function within your program!
  317.  
  318. -------------------------------------------------------------------------------
  319.  
  320. Q. What books are available specifically for WATCOM C/C++?
  321.  
  322. A. None that I know of right now.... anybody care to write one?
  323.  
  324. -------------------------------------------------------------------------------
  325.  
  326. Q. What other extenders are available? How good are they?
  327.  
  328. A. WATCOM C/C++ 10.0 (and prior versions) comes with DOS/4GW, a DOS extender
  329.    from Rational Systems. It is a "freebie" - that is - it does not cost any
  330.    extra, and may be distributed with your programs royalty-free. It does have
  331.    a 32 megabyte limit on memory size, but works with DPMI, VCPI, EMS, XMS,
  332.    and raw. There is also support for virtual memory (using disk for memory).
  333.    It cannot be used to create TSRs.
  334.  
  335.     Rational also has a professional version, which accesses 4 gigabytes of
  336.    memory, ability to generate TSR applications, better support of virtual
  337.    memory and more. (about US$250)
  338.  
  339.      Phar-Lap is the extender of choice for Autodesk and others. Beyond that,
  340.     I have almost no info.
  341.  
  342.      FlashTek is another one I have heard about.
  343.  
  344. -------------------------------------------------------------------------------
  345.  
  346. Q. What considerations do I have using assembly?
  347.  
  348. A. Well, you have more robust versions of the registers to use in protected
  349.    mode. Back in real-mode, a programmer had to use size prefixes to use
  350.    the full 32-bit registers of the 386 CPU. In protected-mode, they will
  351.    *REDUCE* the size of the operands! Thus, all the 66H and 67H references
  352.    (the opcodes of the size modifiers) will need to be stomped out.
  353.    Now, you have "eax", "ebx", "ecx", "edx", "esi", "edi" and so forth. Only
  354.    the "segment" registers remain 16-bit, but on that note, you won't have to
  355.    use them anyway, as they become selectors, and you will see ALL OF YOUR
  356.    MEMORY in the default selectors.
  357.  
  358. -------------------------------------------------------------------------------
  359.  
  360. Q. How do I use DPMI?
  361.  
  362. A. First, dig out the WATCOM C/C++ user's guide and read the
  363.    "INTERRUPT 31H DPMI FUNCTIONS" chapter. Next, be prepared to wrap the
  364.    DPMI functions for easier use. 
  365.  
  366. -------------------------------------------------------------------------------
  367.  
  368. Q. How do I access the VESA BIOS functions?
  369.  
  370. A. First, start off by getting the DPMI stuff down pat.
  371.    Now, allocate a "buffer" (using DPMI function 0x0100) to store data
  372.    you wish to pass back and forth, and use DPMI function 0x0300 to call
  373.    the real-mode int 10H.
  374.    This function will convert any pointers passed to a 32-bit address:
  375.  
  376. void *Dos2Protected(unsigned long dosaddr)
  377. {
  378.     unsigned long a,b;
  379.  
  380.     a=dosaddr>>16;
  381.     b=dosaddr&0x0ffff;
  382.     a<<=4;
  383.     return((void *)(a+b));
  384. }
  385.  
  386.     Also, you can't use the WinFuncPtr functions, as they are real-mode
  387.     routines.
  388.  
  389. -------------------------------------------------------------------------------
  390.  
  391. Q. I bought "Tricks of the Game Programming Gurus" and can't get the code to
  392.    compile at all.
  393.  
  394. A. Well, that, like ACK, YAKICONS, XLIB, and others, involves a major
  395.    overhaul to compile, mostly because of the abundance of 16-bit assembly.
  396.    Strange thing is, much of the assembly could be coded in pure "C" without
  397.    affecting the performance. (this is of course my own opinion).
  398.  
  399. -------------------------------------------------------------------------------
  400. -------------------------------------------------------------------------------
  401.  
  402. More Questions:  Some I will answer in the next edition, but I want to get
  403.                  this posted right now...
  404.  
  405. Q. I *REALLY* need to use a Bimodal interrupt, how do I do it?
  406.  
  407. Q. How do I assemble my .ASM files? What switches using WASM/TASM/MASM?
  408.  
  409.  
  410.  
  411.  
  412. I'm still calling for contributions, criticisms, and corrections.
  413. I don't really want to maintain this (I don't have the time), so when
  414. it reaches version 1.0, I will probably cut back on supporting this.
  415.  
  416.  
  417.                                     Lee
  418.  
  419.                              llorenz@delphi.com
  420.  
  421. Check out my invisible .SIG:
  422. -----------------------------------------------------------------------------
  423.  
  424.  
  425.  
  426. -----------------------------------------------------------------------------
  427.